package UIUtils {
	import flash.display.BitmapData;
	import flash.display.BitmapDataChannel;
	import flash.display.BlendMode;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	
	public class uif_tb {
		private static const WORKCOLOR: uint = 0xFF000000;
		public static function searchSpots(bitmapData: BitmapData, steps: uint = 8, operand: String="=="): Array {
			
			function findRect(a_rect: Rectangle): Boolean {
				 for (var i: int; i<result.length; i++)
					 if (a_rect.equals(result[i].bounds)) return true;
				 return false;
			}
			
			var result: Array = new Array();
			var spots: BitmapData = new BitmapData(bitmapData.width, bitmapData.height, true, 0);
			spots.threshold(bitmapData, bitmapData.rect, new Point(), '>', 0xEE000000, WORKCOLOR, 0xFF000000, false);
			var rect: Rectangle = spots.getColorBoundsRect(0xFFFFFFFF, 0, true);
			
			var step: Number = rect.size.length / steps;
			var x: Number = step / 2;
			var y: Number = step / 2;
			var index: uint = 1;
			var lrect: Rectangle;
			var color: uint;
			while (y < rect.height) {
				while (x < rect.width) {
					color = spots.getPixel32(rect.x + x, rect.y + y);
					if (color != WORKCOLOR) {
						color = 0xFFFF0000 | index;
						spots.floodFill(rect.x + x, rect.y + y, color);
						lrect = spots.getColorBoundsRect(0xFFFFFFFF, color, true);
						if (lrect.size.length > 10) {
							if (!findRect(lrect)) {
								var a_spot: BitmapData = new BitmapData(lrect.width, lrect.height, true, 0);
//								a_spot.copyPixels(spots, lrect, new Point());
								a_spot.threshold(spots, lrect, new Point(), operand, color, 0xFF000000, 0xFFFFFFFF);
								result.push({
									spot 	: a_spot,
									bounds 	: lrect
								});
							}
						}
						index++;
					}
					x += step;
				}
				x = x % rect.width;
				y += step;
			}
			return result;
		}
/*		
		public static function unionSpots(spots: Array, smallPercent: Number): Array {
			var mr: Rectangle = new Rectangle();
			var mspot: Object;
			spots.forEach(function(spot: Object, i: int, arr: Array): void {
				var b: Rectangle = spot.bounds as Rectangle; 
				if (b.size.length > mr.size.length) {
					mr 		= b;
					mspot 	= spot; 
				}
			})
				
			if (mspot) {
				var nrect: Rectangle = mspot.bounds.clone();
				var uspots: Array = new Array();
				var i: int = 0;
				while (i < spots.length) {
					if ((mspot == spots[i]) || 
						(spots[i].bounds.size.length/mr.size.length > smallPercent)) i++;
					else {
						nrect = nrect.union(spots[i].bounds);
						uspots.push(spots[i]);
						spots.splice(i, 1);
					}
				}
				
				var tmpSpot: BitmapData = mspot.spot; 
				var p: Point = mspot.bounds.topLeft.subtract(nrect.topLeft);
				mspot.spot = new BitmapData(nrect.width, nrect.height, true, 0xFF000000);
				mspot.spot.copyChannel(tmpSpot, tmpSpot.rect, p, BitmapDataChannel.ALPHA, BitmapDataChannel.ALPHA);
				mspot.bounds = nrect;
				uspots.forEach(function(item: Object, i: int, arr: Array): void {
					p = item.bounds.topLeft.subtract(nrect.topLeft);
					
					(mspot.spot as BitmapData).merge(item.spot, item.spot.rect, p, 0, 0, 0, 1);	
					item.spot.dispose();
				});
			}
			
			return spots;
		}
*/		
	}
}